import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—08—04
 * Time: 18:19
 */

public class Demo01 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public class Solution {
        public ListNode ReverseList(ListNode head) {
            Stack<ListNode> stack = new Stack<>();
            //入栈
            while(head != null) {
                stack.push(head);
                head = head.next;
            }
            if(stack.isEmpty()) {
                return null;
            }
            //出栈
            ListNode node = stack.pop();
            ListNode newHead = node;
            while(!stack.isEmpty()) {
                ListNode tmpNode = stack.pop();
                node.next = tmpNode;
                node = node.next;
            }
            //走到这里栈空 新链表建成
            node.next = null;
            return newHead;


//         ListNode newHead = null;
//         while(head != null) {
//             ListNode tmp = head.next;
//             //使用头插法
//             head.next = newHead;
//             newHead = head;
//             head = tmp;
//         }
//         return newHead;
        }
    }

    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        if(m==n)//m等于n表示不用反转
            return head;
        ListNode p=head;
        ListNode res=new ListNode(0);//链表的头结点
        ListNode t=res;
        for(int i=1;i<m;i++){//添加不用反转的结点
            t.next=new ListNode(p.val);
            t=t.next;
            p=p.next;
        }
        Stack<ListNode> s1=new Stack<>();
        for(int i=m;i<=n;i++){//存入反转的结点
            s1.push(p);
            p=p.next;
        }
        while(!s1.empty()){//添加反转的结点
            ListNode temp=new ListNode(s1.pop().val);
            t.next=temp;
            t=t.next;
        }
        while(p!=null){//添加不用反转的结点
            t.next=new ListNode(p.val);
            t=t.next;
            p=p.next;
        }
        return res.next;
    }
}

    public ListNode reverseKGroup(ListNode head, int k) {
        //1.创建虚拟头结点
        ListNode newHead = new ListNode(0);
        newHead.next = head;

        //2.记录头结点前面的位置
        ListNode pro = newHead;

        while (head != null) {
            ListNode cur = pro;
            // 查看剩余部分长度是否大于等于 k
            for (int i = 0; i < k; i++) {
                cur = cur.next;
                //剩余长度小于k直接返回
                if (cur == null) {
                    return newHead.next;
                }
            }

            ListNode next = cur.next;
            ListNode[] reverse = myReverse(head, cur);
            head = reverse[0];
            cur = reverse[1];
            // 把子链表重新接回原链表
            pro.next = head;
            cur.next = next;
            pro = cur;
            head = cur.next;
        }

        return newHead.next;
    }

    public ListNode[] myReverse(ListNode head, ListNode tail) {
        ListNode prev = tail.next;
        ListNode p = head;
        while (prev != tail) {
            ListNode nex = p.next;
            p.next = prev;
            prev = p;
            p = nex;
        }
        return new ListNode[]{tail, head};
    }
}
